package com.msmall.community.cookbook.action.html;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.cookbook.service.CookbookManager;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.model.SegmentArticle;
import com.msmall.community.klg.service.ArticleManager;

public class CookbookAction extends HTMLBaseAction{

	public static final String SESSION_ARTICLE_KEY = "SESSION_ARTICLE_KEY";
	
	private static final Log log = LogFactory.getLog(CookbookAction.class);
	
	private static final long serialVersionUID = 4296770733028116013L;

	private Long parentType;
	private Long valueType;
	
	private Long segid;
	private Long cookbookid;
	private Article article;
	private SegmentArticle segmentArticle;
	private CascadeCollection<Segment> current;
	private ArticleManager articleManager;
	private CookbookManager cookbookManager;
	/**
	 * 进入食谱分页面
	 */
	public String list(){
		try {
			if(segid == null || segid == 0)
				segid = ConstantDB.SEGMENT_ID_COOKBOOK_MOTHER_NAV;
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			current = cs.getAllDatas().get(segid);
			
//			page.setPageSize(30);
//			articleManager.getArticlePaginationBySegment(page, getUserid(), segid, true);
//			page.setUrl(ConstantUrl.HTML_COOKBOOK_ARTICLE_FETCH_PAGINATION + "?segid="+ segid);
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}
	/**
	 * 通过segid获得此segid以及此segid下的segment的所有文章
	 */
	public String fetch(){
		try {
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			current = cs.getAllDatas().get(segid);
			
			page.setPageSize(1);
			articleManager.getArticlePaginationBySegment(page, getUserid(), segid, true);
			page.setUrl(ConstantUrl.HTML_COOKBOOK_ARTICLE_FETCH_PAGINATION + "?segid="+ segid);
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}
	/**
	 * 查看单篇文章
	 * @return
	 */
	public String view(){
		try {
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			Object[] objs = articleManager.getArticleAndSegmentByArticle(cookbookid);
			article = (Article) objs[0];
			segmentArticle = (SegmentArticle) objs[1];
			current = cs.getAllDatas().get(segmentArticle.getSegmentid());
			
			page.setPageSize(30);
			articleManager.getArticleReplyPaginationByArticle(page, getUserid(), cookbookid);
			page.setUrl(ConstantUrl.HTML_COOKBOOK_ARTICLE_REPLY_PAGINATION + "?cookbookid="+ cookbookid);
		
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}

	public String replyPage(){
		try {
			page.setPageSize(30);
			articleManager.getArticleReplyPaginationByArticle(page, getUserid(), cookbookid);
			page.setUrl(ConstantUrl.HTML_COOKBOOK_ARTICLE_REPLY_PAGINATION + "?cookbookid="+ cookbookid);
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}
	
	public String create(){
		return SUCCESS;
	}
	
	public String save(){
		try{
			if(!getIsLogin()){
				message = getText("common.authentication.anonymous");
				setToSession(SESSION_ARTICLE_KEY, article);
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			
			int validtitle = articleManager.validateArticleTitle(article.getTitle());
			if(validtitle == 2){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.title.length.short");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			if(validtitle == 3){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.title.length.long");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			if(validtitle == 4){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.title.illegal");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			int validcontent = articleManager.validateArticleContent(article.getContent());
			if(validcontent == 2){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.content.length.short");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			if(validcontent == 3){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.content.length.long");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			if(validcontent == 4){
				setToSession(SESSION_ARTICLE_KEY, article);
				message = getText("html.klg.article.create.content.illegal");
				setToSession(SESSION_MESSAGE_KEY, message);
				return INPUT;
			}
			article.setNickname(getNickname());
			article = articleManager.saveArticle(article, valueType, getUserid(), getUserAgentType());
			removeFromSession(SESSION_ARTICLE_KEY); //清空
			removeFromSession(SESSION_MESSAGE_KEY); //清空
			cookbookid= article.getArticleid();
		}catch(Exception e){
			log.debug("===========>", e);
		}
		return SUCCESS;
	}
	
	public void setArticleManager(ArticleManager articleManager) {
		this.articleManager = articleManager;
	}
	public void setCookbookManager(CookbookManager cookbookManager) {
		this.cookbookManager = cookbookManager;
	}
	public Long getSegid() {
		return segid;
	}
	public void setSegid(Long segid) {
		this.segid = segid;
	}
	public Long getCookbookid() {
		return cookbookid;
	}
	public void setCookbookid(Long cookbookid) {
		this.cookbookid = cookbookid;
	}
	public Article getArticle() {
		return article;
	}
	public void setArticle(Article article) {
		this.article = article;
	}
	public SegmentArticle getSegmentArticle() {
		return segmentArticle;
	}
	public void setSegmentArticle(SegmentArticle segmentArticle) {
		this.segmentArticle = segmentArticle;
	}
	public CascadeCollection<Segment> getCurrent() {
		return current;
	}
	public void setCurrent(CascadeCollection<Segment> current) {
		this.current = current;
	}
	public Long getParentType() {
		return parentType;
	}
	public void setParentType(Long parentType) {
		this.parentType = parentType;
	}
	public Long getValueType() {
		return valueType;
	}
	public void setValueType(Long valueType) {
		this.valueType = valueType;
	}
}
